<!-- src/views/njt/supervisor/index.vue -->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 左侧单位导航栏 -->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>所属单位</span>
            </div>
            <div class="text item">
              <el-menu
                :default-active="organizationId"
                class="el-menu-vertical-demo"
                @select="handleOrgSelect">
                <el-menu-item index="0">
                  <span>全部单位</span>
                </el-menu-item>
                <el-menu-item
                  v-for="org in organizations"
                  :key="org.id"
                  :index="org.id.toString()">
                  <span>{{ org.unitName }}</span>
                </el-menu-item>
              </el-menu>
            </div>
          </el-card>
        </div>
      </el-col>

      <!-- 右侧监管者列表 -->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="手机号码" prop="mobilePhone">
            <el-input
              v-model="queryParams.mobilePhone"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['nongjt:supervisor:add']"
            >新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['nongjt:supervisor:remove']"
            >删除</el-button>
          </el-col>
        </el-row>

        <el-table v-loading="loading" :data="supervisorList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="ID" align="center" prop="id" />
          <el-table-column label="手机号码" align="center" prop="mobilePhone" />
          <el-table-column label="密码" align="center" prop="password" />
          <el-table-column label="所属单位" align="center" prop="organizationName" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['nongjt:supervisor:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['nongjt:supervisor:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改监管者对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户类型" prop="userType">
          <el-select v-model="form.userType" placeholder="请选择用户类型" disabled>
            <el-option label="监管者" value="supervisor" />
          </el-select>
        </el-form-item>
        <el-form-item label="所属单位" prop="organizationId">
          <el-select v-model="form.organizationId" placeholder="请选择所属单位">
            <el-option
              v-for="org in organizations"
              :key="org.id"
              :label="org.unitName"
              :value="org.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号码" prop="mobilePhone">
          <el-input v-model="form.mobilePhone" placeholder="请输入手机号码" maxlength="11" />
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入登录密码" type="password" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupervisors, listOrganizations, addSupervisor, updateSupervisor, delSupervisor } from "@/api/nongjt/supervisor";

export default {
  name: "Supervisor",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监管者表格数据
      supervisorList: [],
      // 当前选中单位ID
      organizationId: "0",
      // 单位列表
      organizations: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mobilePhone: null,
        organizationId: null
      },
      // 表单参数
      form: {
        userType: "supervisor"
      },
      // 表单校验
      rules: {
        organizationId: [
          { required: true, message: "所属单位不能为空", trigger: "change" }
        ],
        mobilePhone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          { pattern: /^1[3-9]\d{9}$/, message: "请输入正确的手机号码", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 5, max: 20, message: "用户密码长度必须介于 5 和 20 之间", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getOrganizations();
  },
  methods: {
    /** 查询监管者列表 */
    getList() {
      this.loading = true;
      listSupervisors(this.queryParams).then(response => {
        this.supervisorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询单位列表 */
    getOrganizations() {
      listOrganizations().then(response => {
        this.organizations = response.data;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择单位 */
    handleOrgSelect(index) {
      this.organizationId = index;
      this.queryParams.organizationId = index === "0" ? null : parseInt(index);
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加监管者";
      this.form = {
        userType: "supervisor"
      };
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "修改监管者";
      this.form = {
        id: row.id,
        mobilePhone: row.mobilePhone,
        organizationId: row.organizationId,
        userType: "supervisor"
      };
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSupervisor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupervisor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除监管者编号为"' + ids + '"的数据项？').then(function() {
        return delSupervisor(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: null,
        userType: "supervisor",
        organizationId: null,
        mobilePhone: null,
        password: null
      };
      this.resetForm("form");
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    }
  }
};
</script>
